<?php
namespace app\common\logic;
use app\common\oss\OssUtil;
use Qiniu\Auth as Auth;
use Qiniu\Storage\UploadManager;

class UploadLogic extends Logic{

    //上传函数
    static function savepic($post,$prefix,$folder,$suffix = ".jpeg"){
        $picname = $prefix.uniqid().$suffix;        
        //本地保存图片路径
        if (!is_dir('uploads')) {
            mkdir('uploads');
        }
        if (!is_dir('uploads/temp')) {
            mkdir('uploads/temp');
        }
        $file = ROOT_PATH.'public_html/uploads/temp/'.$picname;       
        $base64 = base64_decode($post);
        $save = file_put_contents($file, $base64);
        if($save){
            //保存成功，上传到OSS
            $oss_config = config('aliyun_oss');
            $oss_file = 'wz/'.$folder.'/'.$picname;
            $local_file = 'uploads/temp/'.$picname;
            $OssUtil = new OssUtil();
            $res = $OssUtil->uploadFile($oss_config['Bucket'], $oss_file, $local_file);
            unlink($file);
            return $oss_file;
        }
    }

    //上传本地函数
    static function savepictolocal($post,$prefix,$suffix = ".jpeg"){
        $picname = $prefix.uniqid().$suffix;        
        //本地保存图片路径
        if (!is_dir('uploads')) {
            mkdir('uploads');
        }
        if (!is_dir('uploads/images')) {
            mkdir('uploads/images');
        }        
        $file = ROOT_PATH.'public_html/uploads/images/'.$picname;       
        $base64 = base64_decode($post);
        $save = file_put_contents($file, $base64);
        if($save){
            return "uploads/images/".$picname;
        }
    }

    /**
     * 七牛云上传
     * @param $prefix 目录前缀
     * @param $name <input type="file">name值
     * @param string $suffix 文件后缀名
     * @return array|\think\response\Json
     * @throws \Exception
     * return json(上传文件完整URL)
     */
    static function uploadQiniu($prefix, $name, $suffix = ".jpeg")
    {
        if (request()->isPost()) {
            $file = request()->file($name);
            // 要上传图片的本地路径
            $filePath = $file->getRealPath();
            $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
            // 上传到七牛后保存的文件名
            $key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $suffix;
            require_once ROOT_PATH . 'vendor/qiniu-php-sdk-7.2.6/autoload.php';
            // 需要填写你的 Access Key 和 Secret Key
            $accessKey = config('Qiniuyun_conf.AccessKey');
            $secretKey = config('Qiniuyun_conf.SecretKey');
            // 构建鉴权对象
            $auth = new Auth($accessKey, $secretKey);
            // 要上传的空间
            $bucket = config('Qiniuyun_conf.Bucket');
            $domain = config('Qiniuyun_conf.Domain');
            $token = $auth->uploadToken($bucket);
            // 初始化 UploadManager 对象并进行文件的上传
            $uploadMgr = new UploadManager();
            // 调用 UploadManager 的 putFile 方法进行文件的上传
            list($ret, $err) = $uploadMgr->putFile($token, $prefix. "/" . $key, $filePath);
            if ($err !== null) {
                return ["err" => 1, "msg" => $err, "data" => ""];
            } else {
                //返回图片的完整URL
                $result = [
                    "err" => 0,
                    "msg" => "上传成功",
                    "data" => $domain. $ret['key']
                ];
                return json($result);
            }
        }
    }

}